﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CMP.ERP.Model;
using CMP.ERP.Helper;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace CMP.ERP.DAL
{
    public class FahuoDAL
    {
        SQLHelper h = new SQLHelper();
        string connSql = ConfigurationManager.ConnectionStrings["ConnMySql"].ToString();
        /// <summary>
        /// 根据Id获取单行数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Shipments GetShipmentsById(int id)
        {
            string sql = "select Shipments_Id,Shipments_data,Shipments_Per,Shipments_Phone,Shipments_State,Express_Company_Name,WareHose_Name,Shipments_SumMoney from Shipments join WareHose on Shipments.WareHose_Id=WareHose.WareHose_Id  join ExpressCompany on Shipments.Express_Company_Id=ExpressCompany.Express_Company_Id where Shipments_Id="+id+"";
            SqlDataAdapter adapter = new SqlDataAdapter(sql, connSql);
            DataTable table = new DataTable();
            adapter.Fill(table);
            Shipments s = new Shipments();
            foreach (DataRow item in table.Rows)
            {
                s.Shipments_Id = Convert.ToInt32(item["Shipments_Id"]);
                s.Shipments_data = item["Shipments_data"].ToString();
                s.Shipments_Per = item["Shipments_Per"].ToString();
                s.Shipments_Phone = item["Shipments_Phone"].ToString();
                s.Shipments_State = item["Shipments_State"].ToString();
                s.Express_Company_Name = item["Express_Company_Name"].ToString();
                s.WareHose_Name = item["WareHose_Name"].ToString();
                s.Shipments_SumMoney = Convert.ToInt32(item["Shipments_SumMoney"]);
            }
            return s;
        }
        /// <summary>
        /// 修改订单状态及添加发货单
        /// </summary>
        /// <param name="f"></param>
        /// <returns></returns>
        public int UpdStateAndAddFahuolist(Fahuolist f)
        {
            string ProcName = "Proc_AddFahuo";
            SqlParameter[] par=new SqlParameter[]
            {
                new SqlParameter("@Shipments_Id",f.Shipments_Id),
                new SqlParameter("@Shipments_data",f.Shipments_data),
                new SqlParameter("@Shipments_Per",f.Shipments_Per),
                new SqlParameter("@Shipments_Phone",f.Shipments_Phone),
                new SqlParameter("@Shipments_State",f.Shipments_State),
                new SqlParameter("@Express_Company_Name",f.Express_Company_Name),
                new SqlParameter("@WareHose_Name",f.WareHose_Name),
                new SqlParameter("@Shipments_SumMoney",f.Shipments_SumMoney)
            };
            return h.ExecNonquery_Proc(ProcName, par);
        }
        /// <summary>
        /// 发货单显示
        /// </summary>
        /// <returns></returns>
        public DataTable FahuolistShow()
        {
            string sql = "select * from Fahuolist";
            return h.ExecDataTable(sql);
        }
        /// <summary>
        /// 确认收货
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int shouhuo(int id)
        {
            string ProcName = "Proc_Shouhuo";
            SqlParameter[] par = new SqlParameter[1]
            {
                new SqlParameter("@FahuoId",id)
            };
            return h.ExecNonquery_Proc(ProcName, par);
        }
        /// <summary>
        /// 显示已发货的订单表
        /// </summary>
        /// <returns></returns>
        public DataTable GetShipments()
        {
            string sql = "select Shipments_Id,Shipments_data,Shipments_Per,Shipments_Phone,Shipments_State,Express_Company_Name,WareHose_Name,Shipments_SumMoney from Shipments s join WareHose w on s.WareHose_Id=w.WareHose_Id join ExpressCompany e on e.Express_Company_Id=s.Express_Company_Id where Shipments_State='已发货' order by Shipments_data DESC";
            return h.ExecDataTable(sql);
        }
    }
}
